package com.ymatou.iapi.hunter.testcase;

import java.text.ParseException;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterInfoByCodeRequest;
import com.ymatou.iapi.hunter.service.GetHunterInfoByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetHunterInfoByCode {

	private static GetHunterInfoByCodeRequest getHunterInfoByCodeRequest;
	private static GetHunterInfoByCodeCall getHunterInfoByCodeCall;
	HunterRelationWrapper wrapper = new HunterRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("登录时通过邀请码绑定永久关系");
	}

	@Before
	public void caseUp() {
		getHunterInfoByCodeRequest = new GetHunterInfoByCodeRequest();
		getHunterInfoByCodeCall = new GetHunterInfoByCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterInfoByCodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb(JSONObject expectObject) throws JSONException, ParseException {

		HunterRelationWrapper wrapper = new HunterRelationWrapper();
		HunterRelation actualHunterRelation = wrapper
				.selectHunterInfoByCode(getHunterInfoByCodeRequest.getCode());

		Logger.verifyEquals(expectObject.getLong("hunterId"),
				actualHunterRelation.getHunter_id(), "验证hunterId");
		Logger.verifyEquals(expectObject.getInt("hunterLevel"),
				actualHunterRelation.getHunter_level(), "验证hunterLevel");
		Logger.verifyEquals(expectObject.getInt("hunterStatus"),
				actualHunterRelation.getStatus(), "验证hunterStatus");
		Logger.verifyEquals(expectObject.getString("addTime"),
				YMTDateUtil.getFromDTSDate(actualHunterRelation
						.getAdd_time().toString(), YMTDateUtil.YMDHMS), "验证addTime");
		Logger.verifyEquals(expectObject.getInt("childrenTotal"),
				actualHunterRelation.getChildren_total(), "验证childrenTotal");

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterInfoByCode_001() {
		Logger.start(true, "通过邀请码绑定成功");
		try {

			getHunterInfoByCodeRequest.setCode("KHL4YJ");

			getHunterInfoByCodeCall.setData(getHunterInfoByCodeRequest);
			getHunterInfoByCodeCall.callService();

			Logger.verifyEquals("success", getHunterInfoByCodeCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterInfoByCodeCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(true, getHunterInfoByCodeCall.getResponseData()
					.getBoolean("success"), "验证success");

			verifyDb(getHunterInfoByCodeCall.getResult());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterInfoByCode_002() {
		Logger.start(true, "邀请码为空");
		try {

			getHunterInfoByCodeRequest.setCode("");

			getHunterInfoByCodeCall.setData(getHunterInfoByCodeRequest);
			getHunterInfoByCodeCall.callService();

			Logger.verifyEquals("code:邀请码不能为空", getHunterInfoByCodeCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterInfoByCodeCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, getHunterInfoByCodeCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterInfoByCode_003() {
		Logger.start(true, "推手不存在");
		try {

			getHunterInfoByCodeRequest.setCode("1");

			getHunterInfoByCodeCall.setData(getHunterInfoByCodeRequest);
			getHunterInfoByCodeCall.callService();

			Logger.verifyEquals("推手不存在！", getHunterInfoByCodeCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3, getHunterInfoByCodeCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, getHunterInfoByCodeCall.getResponseData()
					.getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
